<?php

namespace app\common\service;

use JPush\Client as JPush;

class PushService
{
    const PUSH_TYPE_SINGLE         = 1;//推送个人
    const PUSH_TYPE_TAG            = 2;//推送tag
    const PUSH_TYPE_SIGNLE_MESSAGE = 3;//推送个人 message类型

    public static function getConfigList()
    {
        $list = [
            ['app_key' => '2d2ca3723881a1a2cd171a80', 'master_secret' => 'da6af5c2ec5b26d2b03adfae'],//主包
            ['app_key' => '935f4b389a9a29eeff2a887e', 'master_secret' => '0478956b2883163bc8c253cf'],//4399
            ['app_key' => '47e980034b694f76d9d83f90', 'master_secret' => 'b23c9860a737d837316017ff'],//vivo
            ['app_key' => '3c81b8da176954c6d045f9de', 'master_secret' => '9234b8d66e9a109cfc44cea2'],//mi
            ['app_key' => '9e57b7eb7fa718694191bcf9', 'master_secret' => '3179b80aff23924da48d09b7'],//oppo
            ['app_key' => 'e2c895ad9f32019c6caf5e79', 'master_secret' => 'e61410e86af14073cea54ae6'],//233
        ];
        return $list;
    }

    //按Tag推送
    public static function pushTag($config, $data, $tag, $type = 1)
    {
        $app_debug = config('is_dev');

        $app_key       = $config['app_key'];
        $master_secret = $config['master_secret'];

        $client = new JPush($app_key, $master_secret);

        try {
            if ($app_debug) {
                $response = $client->push()
                    ->setPlatform('all')
                    ->addTag(['debug', $tag])
                    ->setMessage('data', 'trading', 'text', array(
                        'type' => $type,
                        'data' => $data
                    ))
                    ->send();
                return [
                    'result' => 'success'
                ];
            } else {
                $response = $client->push()
                    ->setPlatform('all')
                    ->addTag([$tag])
                    ->setMessage('data', 'trading', 'text', array(
                        'type' => $type,
                        'data' => $data
                    ))
                    ->send();
                return [
                    'result' => 'success'
                ];
            }
        } catch (\JPush\Exceptions\APIConnectionException $e) {
            return [
                'code'    => '1000',
                'message' => 'api connect fail！' . $e->getMessage()
            ];
        } catch (\JPush\Exceptions\APIRequestException $e) {
            return [
                'code'    => '2000',
                'message' => 'api request fail！' . $e->getMessage()
            ];
        }
    }

    //推送单人
    public static function pushSingle($config, $alias, $content, $data, $title = '', $type = 1)
    {
        $app_debug = config('is_dev');

        $app_key       = $config['app_key'];
        $master_secret = $config['master_secret'];

        $client = new JPush($app_key, $master_secret);

        try {
            if ($app_debug) {
                $response = $client->push()
                    ->setPlatform('all')
                    ->addAlias($alias)
                    ->iosNotification($content, array(
                        'alert'    => $content,
                        'sound'    => 'sound.caf',
                        'category' => 'jiguang',
                        'extras'   => array(
                            'title'   => $title,
                            'type'    => $type,
                            'content' => $content,
                            'data'    => $data
                        ),
                    ))
                    ->androidNotification($content, array(
                        'alert'  => $content,
                        'title'  => $title,
                        'extras' => array(
                            'title'   => $title,
                            'type'    => $type,
                            'content' => $content,
                            'data'    => $data
                        ),
                    ))
                    ->options(array(
                        // apns_production: 表示APNs是否生产环境，
                        // True 表示推送生产环境，False 表示要推送开发环境；如果不指定则默认为推送生产环境
                        'apns_production' => false,
                    ))
                    ->send();
                return [
                    'result' => 'success'
                ];
            } else {
                $response = $client->push()
                    ->setPlatform('all')
                    ->addAlias($alias)
                    ->iosNotification($content, array(
                        'alert'    => $content,
                        'sound'    => 'sound.caf',
                        'category' => 'jiguang',
                        'extras'   => array(
                            'title'   => $title,
                            'type'    => $type,
                            'content' => $content,
                            'data'    => $data
                        ),
                    ))
                    ->androidNotification($content, array(
                        'alert'  => $content,
                        'title'  => $title,
                        'extras' => array(
                            'title'   => $title,
                            'type'    => $type,
                            'content' => $content,
                            'data'    => $data
                        ),
                    ))
                    ->options(array(
                        // apns_production: 表示APNs是否生产环境，
                        // True 表示推送生产环境，False 表示要推送开发环境；如果不指定则默认为推送生产环境
                        'apns_production' => true,
                    ))
                    ->send();
                return [
                    'result' => 'success'
                ];
            }
        } catch (\JPush\Exceptions\APIConnectionException $e) {
            return [
                'code'    => '1000',
                'message' => 'api connect fail！' . $e->getMessage()
            ];
        } catch (\JPush\Exceptions\APIRequestException $e) {
            return [
                'code'    => '2000',
                'message' => 'api request fail！' . $e->getMessage()
            ];
        }
    }

    //推送个人  信息类型
    public static function pushSingleMessage($config, $alias, $content, $data, $title = '', $type = 1)
    {
        $app_debug = config('is_dev');

        $app_key       = $config['app_key'];
        $master_secret = $config['master_secret'];

        $client = new JPush($app_key, $master_secret);

        try {
            if ($app_debug) {
                $response = $client->push()
                    ->setPlatform('all')
                    ->addAlias($alias)
                    ->setMessage($content, $title, 'text', array(
                        'title'   => $title,
                        'type'    => $type,
                        'content' => $content,
                        'data'    => $data
                    ))
                    ->send();
                return [
                    'result' => 'success'
                ];
            } else {
                $response = $client->push()
                    ->setPlatform('all')
                    ->addAlias($alias)
                    ->setMessage($content, $title, 'text', array(
                        'title'   => $title,
                        'type'    => $type,
                        'content' => $content,
                        'data'    => $data
                    ))
                    ->send();
                return [
                    'result' => 'success'
                ];
            }
        } catch (\JPush\Exceptions\APIConnectionException $e) {
            return [
                'code'    => '1000',
                'message' => 'api connect fail！' . $e->getMessage()
            ];
        } catch (\JPush\Exceptions\APIRequestException $e) {
            return [
                'code'    => '2000',
                'message' => 'api request fail！' . $e->getMessage()
            ];
        }
    }


    //推送全部用户
    public static function pushAll($config, $content, $data, $title = '', $type = 1)
    {
        $app_debug = config('is_dev');

        $app_key       = $config['app_key'];
        $master_secret = $config['master_secret'];

        $client = new JPush($app_key, $master_secret);

        try {
            if ($app_debug) {
                $response = $client->push()
                    ->setPlatform('all')
                    ->setAudience('all')
                    ->addTag('debug')
                    ->iosNotification($content, array(
                        'alert'    => $content,
                        'sound'    => 'sound.caf',
                        'category' => 'jiguang',
                        'extras'   => array(
                            'title'   => $title,
                            'type'    => $type,
                            'content' => $content,
                            'data'    => $data
                        ),
                    ))
                    ->androidNotification($content, array(
                        'alert'  => $content,
                        'title'  => $title,
                        'extras' => array(
                            'title'   => $title,
                            'type'    => $type,
                            'content' => $content,
                            'data'    => $data
                        ),
                    ))
                    ->options(array(
                        // apns_production: 表示APNs是否生产环境，
                        // True 表示推送生产环境，False 表示要推送开发环境；如果不指定则默认为推送生产环境
                        'apns_production' => false,
                    ))
                    ->send();
                return [
                    'result' => 'success'
                ];
            } else {
                $response = $client->push()
                    ->setPlatform('all')
                    ->setAudience('all')
                    ->iosNotification($content, array(
                        'alert'    => $content,
                        'sound'    => 'sound.caf',
                        'category' => 'jiguang',
                        'extras'   => array(
                            'title'   => $title,
                            'type'    => $type,
                            'content' => $content,
                            'data'    => $data
                        ),
                    ))
                    ->androidNotification($content, array(
                        'alert'  => $content,
                        'title'  => $title,
                        'extras' => array(
                            'title'   => $title,
                            'type'    => $type,
                            'content' => $content,
                            'data'    => $data
                        ),
                    ))
                    ->options(array(
                        // apns_production: 表示APNs是否生产环境，
                        // True 表示推送生产环境，False 表示要推送开发环境；如果不指定则默认为推送生产环境
                        'apns_production' => true,
                    ))
                    ->send();
                return [
                    'result' => 'success'
                ];
            }
        } catch (\JPush\Exceptions\APIConnectionException $e) {
            return [
                'code'    => '1000',
                'message' => 'api connect fail！' . $e->getMessage()
            ];
        } catch (\JPush\Exceptions\APIRequestException $e) {
            return [
                'code'    => '2000',
                'message' => 'api request fail！' . $e->getMessage()
            ];
        }
    }

    //推送全部用户  消息类型
    public static function pushAllMessage($config, $content, $data, $title = '', $type = 1)
    {
        $app_debug = config('is_dev');

        $app_key       = $config['app_key'];
        $master_secret = $config['master_secret'];

        $client = new JPush($app_key, $master_secret);

        try {
            if ($app_debug) {
                $response = $client->push()
                    ->setPlatform('all')
                    ->setAudience('all')
                    ->setMessage($content, $title, 'text', array(
                        'title'   => $title,
                        'type'    => $type,
                        'content' => $content,
                        'data'    => $data
                    ))
                    ->send();
                return [
                    'result' => 'success'
                ];
            } else {
                $response = $client->push()
                    ->setPlatform('all')
                    ->setAudience('all')
                    ->setMessage($content, $title, 'text', array(
                        'title'   => $title,
                        'type'    => $type,
                        'content' => $content,
                        'data'    => $data
                    ))
                    ->send();
                return [
                    'result' => 'success'
                ];
            }
        } catch (\JPush\Exceptions\APIConnectionException $e) {
            return [
                'code'    => '1000',
                'message' => 'api connect fail！' . $e->getMessage()
            ];
        } catch (\JPush\Exceptions\APIRequestException $e) {
            return [
                'code'    => '2000',
                'message' => 'api request fail！' . $e->getMessage()
            ];
        }
    }
}